<?php

class Util {
	/*private static $_timers = array();
	
	public static function timer($name = null) {
		if(!isset(self::$_timers[$name])) {
			self::$_timers[$name] = microtime(true);
			return 0;
		} else {
			$t = self::$_timers[$name];
			self::$_timers[$name] = microtime(true);
			return microtime(true) - $t;
		}
	}
	
	public static function memory() {
		return memory_get_usage();
	}*/
	
	public static function createBenchCode($name, &$code, $key) {
		//if(file_exists(BENCH_DIR."/tmp/bench_".$name.".".$key.".php")) 
		//	return true;
		
		foreach($code["tests"] as $k => &$test) {
			$r = '<?php'."\n//".date("Y-m-d H:i:s")."\n";
			if(!empty($code["prepare"]))
				$r .= $code["prepare"];
			$r .= "\n".'$results = array();'."\n";
			if(!empty($test["script"]) || !empty($test["script"]))
				continue;
			if(!empty($test["prepare"]))
				$r .= $test["prepare"]."\n";
			$r .= "\n//Test $k\n".'$time = microtime(true);'."\n".'$memory = memory_get_usage();'."\n";
			if(!empty($test["i"]) && $test["i"] > 1) {
				$r .= 'for($i=0; $i<'.intval($test["i"]).'; $i++) { // try '.$test["i"].' iteration'."\n\t".'//Test body'."\n".
				"\t".str_replace("\n","\n\t",$test["code"])."\n".'}'."\n";
			} else {
				$r .= "\t".$test["code"];
			}
			$r .= "\n//Save result\n".'$time = microtime(true) - $time;'."\n".'$memory = memory_get_usage() - $memory; $results["'.$k.'"] = array("time" => $time, "memory" => $memory);'."\n";
			if(!empty($code["after"]))
			$r .= "\n".$code["after"];
			$r .= "\n".'return $results;'."\n".'?>';
			$fp = fopen(BENCH_DIR."/tmp/bench_".$name.".".$k.".php", "w+");
			
			if($fp) {
				fwrite($fp, $r);
				fclose($fp);
			} else {
				throw new Exception("Unable to write (".BENCH_DIR."/tmp/)");
				//die("Unable to write");
				//return false;
			}
		}
		
		return true;
		//return file_put_contents(BENCH_DIR."/tmp/bench_".$name.".php", $r);
	}
	
	public static function syntaxCheck($name, &$error_message) {
		if(!file_exists(BENCH_DIR."/tmp/bench_".$name.".php")) {
			$error_message = "Benchfile not found in ".BENCH_DIR."/tmp (must be writable)";
			return false;
		}
		//return php_check_syntax(BENCH_DIR."/tmp/bench_".$name.".php", $error_message) ;
		return true;
	}
	
	public static function removeTemp($name = null) {
		if(!$name) {
			$name = "*";
		} else {
			$name = "bench_".$name.".php";
		}
		$files = glob(BENCH_DIR."/tmp/".basename($name));
		foreach($files as $file) {
			unlink($file);
		}
	}
	
	public static function isBenchExists($bench, $key = 0) {
		return file_exists(BENCH_DIR."/tmp/bench_".$bench.".php");
	}
	
	public static function getBenchResult($bench) {
		return include(BENCH_DIR."/tmp/bench_".$name.".php");
	}
	
	public static function highlightFile($bench) {
		return highlight_file(BENCH_DIR."/tmp/bench_".basename($bench).".php", true);
	}
	
	public static function prepareBench(&$bench) {
		$bench["_script"] = 0;
		$bench["_code"] = 0;
		foreach($bench["tests"] as $k => $test) {
			if(!empty($test["script"])) {
				if(file_exists(BENCH_DIR."/benches/".$test["script"].".php")) {
					$script_bench = include(BENCH_DIR."/benches/".$test["script"].".php");
					if(!isset($script_bench["tests"][$k])) {
						$bench["tests"][$k]["error"] = "Test $k not found";
						continue;
					}
					if($script_bench["prepare"]) {
						$bench["script_prepare"][$k] = $script_bench["prepare"];
						$bench["tests"][$k] = $bench["tests"][$k] + $script_bench["tests"][$k];
					}
					$bench["_script"]++;
				} else {
					$bench["tests"][$k]["error"] = "Script not found";
				}
			} else {
				$bench["_code"]++;
			}
		}
	}
	
	public static function getBenchList() {
		if(file_exists(BENCH_DIR."/tmp/opconf.php"))
			$opconf = include(BENCH_DIR."/tmp/opconf.php");
		$mktime = filemtime(BENCH_DIR."/configs/bench.php");
		if(!$opconf || $opconf["mktime"] != $mktime) {
			
		}
		
	}
}
?>
